<?php
include("common/common.php");
session_start();

function gen_randomstr( $n=8 ) {
	return substr(str_shuffle('abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'), 0, $n);
}

$email = $_POST["pwdemail"];
$ok = false;
$sitemail = null;
$newpwd = null;
$prenom = null;
if (!is_null($email)){
	if (filter_var($email, FILTER_VALIDATE_EMAIL)){
		$db = Database::obtain(DB_SERVER, DB_USER, DB_PASS, DB_DATABASE);
		$db->connect();
		$sql = "SELECT email,prenom FROM `user_info`
		WHERE email = '".$email."'";
		$record = $db->query_first($sql);
		if (isset($record['email'])){
			$prenom = $record['prenom'];
			$newpwd = gen_randomstr();
			$data["psword"] = $newpwd;
			$db->update("user_info", $data,"email='".$email."'");
			$ok = true;
		}
		$sql2 = "SELECT email FROM `website`";
		$sitemail = $db->query_first($sql2);
		$db->close();
	}
}
if($ok){
	//send pwd
	$root_path=get_root_path();
	$_lang = null;
	if (is_null($_lang) and isset($_COOKIE['_lang'])){
		$_lang=$_COOKIE['_lang'];
	}else{
		if(is_null($_lang)){
			$_lang = FR;
		}
	}
	
	$message ='
	<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title>kacho-resto</title>
	</head>
	<body>
	<div style="background:url(http://kacho-resto.com/mail/images/mail-bg.png);width:650px;overflow:hidden;position:relative;height:500px">
	<div style="width:650px; background:url(http://kacho-resto.com/mail/images/headBg.png) repeat-x; height:147px; position:absolute; z-index:10">
	<div style="height:147px">
	<h1 style="background:url(http://kacho-resto.com/mail/images/logo.png) no-repeat;width:191px; height:68px; margin:30px 0 0 20px;float:left"></h1>
		
	';
	if($_lang==FR){
		$message .='
		<div style="width:400px; height:45px; overflow:hidden; left:225px; top:45px; position:absolute; font-size:18px; color:#AEAEAE;font-family:Lucida Handwriting"></div>
		<div style="width:400px; height:20px; overflow:hidden; left:225px; position:absolute; font-size:18px; color:#AEAEAE; top:55px;font-family:Lucida Handwriting">';
		$message .= "
		Votre 1er portail franco-arabe de restauration méditerranéenne
		";
	}else{
		$message .='
		<div style="width:400px; height:45px; overflow:hidden; right:25px; top:45px; position:absolute; font-size:18px; color:#AEAEAE;font-family:Lucida Handwriting"></div>
		<div style="width:400px; height:20px; overflow:hidden; text-align:right; right:25px; position:absolute; font-size:18px; color:#AEAEAE; top:55px;font-family:Lucida Handwriting">';
		$message .= "
		موقعكم الوحيد باللغة العربية لعالم المأكولات والضيافة المغربية والشرق أوسطية
		";
	}
     
     $message .='
  </div>
 </div>
</div>
<!--header end of-->
';
$message .='<div style="width:650px; height:500px;filter:progid:DXImageTransform.Microsoft.gradient(startcolorstr=#99ffffff,endcolorstr=#99ffffff); position:absolute; top:147px">
  <h3 style="padding:10px 10px 0 20px;font-size:13px;font-family:Helvetica Neue, regular">';
	if($_lang==FR){
 		$message .='Bonjour ';
 		$message.= $prenom.',</h3>';
     }else{
     	$message.= '،'.$prenom.' ';
     	$message .='مرحباً ';
     	$message.='</h3>';
     }
//common
  
  
  if($_lang==FR){
   $message.='<p style="padding:0 0 0 20px;font-size:13px;font-family:Helvetica Neue, regular">Vous avez demandé un <b>nouveau mot de passe</b> pour votre compte sur KachoResto.</p>
   <p style="padding:10px 0 0 20px;font-size:13px;font-family:Helvetica Neue"><b>Identifiant : <label style="color:#4c7b00">'.$email.'</label></b></p>
   <p style="padding:0 0 0 20px;font-size:13px;font-family:Helvetica Neue"><b>Mot de passe : <label style="color:#4c7b00">'.$newpwd.'</label></b></p>
   <p style="padding:10px 0 0 20px;font-size:13px;font-family:Helvetica Neue, regular">Vous pouvez maintenant vous connecter et modifier ce nouveau mot de passe dans votre espace client.</p>';
  }else{
  	$message.='<p style="padding:0 0 0 20px;font-size:13px;font-family:Helvetica Neue, regular">.طلبتم كلمة مرورجديدة</p>
  	<p style="padding:10px 0 0 20px;font-size:13px;font-family:Helvetica Neue"><b>Identifiant : <label style="color:#4c7b00">'.$email.'</label></b></p>
  	<p><table style="margin-left:20px;font-size:13px;font-family:Helvetica Neue"><tr><td><b>كلمة المرور : </b></td><td><b><label style="color:#4c7b00">'.$newpwd.'</label></b></td></tr></table></p>
  	<p style="padding:10px 0 0 20px;font-size:13px;font-family:Helvetica Neue, regular">.يمكنكم إستعمال كلمة المرورجديدة</p>';
  }
   
  
//common  
  $message .='<ul style="{width:600px; margin:30px auto">
     <li style="height:48px;margin-top:20px;margin-bottom:10px; font-size:12px"><img style="vertical-align:middle; margin-right:20px" src="http://kacho-resto.com/mail/images/logoPic_1.png" width="83" height="48" />';

     if($_lang==FR){
        $message .='A bientôt sur';
        $message .= '<a href="'.$root_path.'" style="color:#333; text-decoration: underline"> kacho-resto.com</a> !</li>';
     }else{
     	$message .= '! <a href="'.$root_path.'" style="color:#333; text-decoration: underline">kacho-resto.com</a> ';
     	$message .='الى اللقاء على';
     	$message .= '</li>';
     }
      
     $message .= '<li style="margin-right:10px;float:right">
         By <a href="'.$root_path.'kac.php?menu=3" style="color:#ff6700; text-decoration: underline">Kachmir Consultancy</a>
     </li>
   </ul>
</div>
<!--regEmail end of-->
</div>
</body>
</html>
';
	
	$to = $email;
	$subject = "www.kacho-resto.com";
	$from = $sitemail['email'];
	sendmail($from, $to, $subject, $message);
}
$_SESSION["sendpwdtag"]=1;
$lasturl = $_SESSION['currenturl'];
redirect($lasturl);

?>